<?php
namespace Api\Controller;
use Think\Controller\RestController;

/**
 * Person Related Api Class
 *
 * @package Api\Controller
 * @author lythesia
**/
class PersonController extends RestController {
  protected $allowMethod = array('get');
  protected $allowType   = array('json', 'xml');

  /**
   * Get single person information by `person_id`
   *
   * @return mixed  return query results on `success` or error message on `fail`
   * @author lythesia
   **/
  public function info() {
    $pid = I('get.id', '', 'trim,filter_personid');
    $default_field = 'id,name,aka,birthdate,deathdate,gender,occupation,resume,poster';
    $field = I('get.include', $default_field, 'trim');
    $format = I('get.format', 'json', 'trim,filter_api_format');
    $data = array();

    $data['status'] = 'fail';
    if(!$pid) {
      $data['msg'] = 'Invalid ID format, please check API manual.';
      goto end;
    }
    if($field !== '*' && !filter_person_field($field)) {
      $data['msg'] = 'Invalid fields, please check API manual.';
      goto end;
    }
    if($format === false) {
      $data['msg'] = 'Invalid format, only `json` and `xml` are allowed.';
      $format = 'json';
      goto end;
    }

    $data['data'] = A('Common/Person')->getPersonById($pid, $field);
    if($data['data']) {
      $data['status'] = 'success';
    }
    else {
      unset($data['data']);
      $data['msg'] = 'Database query failed or no result found.';
    }

    end:
    $this->response($data, $format);
  }

  /**
   * Get single person with specified related information(e.g genres movies ..) by `person_id`
   *
   * @return mixed return query results on `success` or error message on `fail`
   * @author lythesia
   **/
  public function info_rel() {
    $pid = I('get.id', '', 'trim,filter_personid');
    $default_field = 'id,name,aka,birthdate,deathdate,gender,occupation,resume,poster';
    $field = I('get.include', $default_field, 'trim');
    $rel = I('get.rel', '', 'trim');
    $format = I('get.format', 'json', 'trim,filter_api_format');
    $data = array();

    $data['status'] = 'fail';
    if(!$pid) {
      $data['msg'] = 'Invalid ID format, please check API manual.';
      goto end;
    }
    if($field !== '*' && !filter_person_field($field)) {
      $data['msg'] = 'Invalid fields, please check API manual.';
      goto end;
    }
    if($rel !== '' && !filter_person_rel($rel)) {
      $data['msg'] = 'Invalid relation field, please check API manual.';
      goto end;
    }
    if($format === false) {
      $data['msg'] = 'Invalid format, only `json` and `xml` are allowed.';
      $format = 'json';
      goto end;
    }

    $opt = array(
      'rel' => explode(',', $rel),
    );
    $data['data'] = A('Common/Person')->getPersonRelById($mid, $opt, $field);
    if($data['data']) {
      $data['status'] = 'success';
    }
    else {
      unset($data['data']);
      $data['msg'] = 'Database query failed or no result found.';
    }

    end:
    $this->response($data, $format);
  }
}
